package org.song.videoplayer.floatwindow.hos;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.sysappcomponents.settings.AppSettings;
import ohos.utils.IntentConstants;
import org.song.videoplayer.floatwindow.FloatMoveView;
import org.song.videoplayer.floatwindow.FloatParams;

public class FloatWindowHelp {

    private FloatParams floatParams, newFloatParams;
    private Context context;
    private FloatMoveView floatMoveView;
    private int type;
    private CommonDialog commonDialog;
    private final int REQUEST_CODE = 1000;
    private Window window;
    private WindowManager.LayoutConfig params;

    public FloatWindowHelp(Context context) {
        this.context = context;
        type = WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY;
    }

    public boolean enterWindowFloat(Component view, final FloatParams floatParams) {
        if (floatParams.systemFloat && !checkPermission()) {
            Intent intent = new Intent();
            Intent.OperationBuilder operationBuilder = new Intent.OperationBuilder();
            operationBuilder.withAction(IntentConstants.ACTION_WIRELESS_SETTINGS);
            intent.setOperation(operationBuilder.build());
            intent.addFlags(Intent.FLAG_ABILITY_CONTINUATION);
            context.startAbility(intent,REQUEST_CODE);
            return false;
        }
        this.floatParams = floatParams;
        newFloatParams = floatParams.clone();
        if (floatParams.systemFloat){
            params = new WindowManager.LayoutConfig();
            params.type = WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY;
            params.alignment = LayoutAlignment.CENTER;
            params.flags = WindowManager.LayoutConfig.MARK_TOUCHABLE_IMPOSSIBLE | WindowManager.LayoutConfig.MARK_WATCH_OUTSIDE_TOUCH;
            params.height = floatParams.h;
            params.width = floatParams.w;

            window = WindowManager.getInstance().addComponent((ComponentContainer) view, context, WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY);
            window.setLayoutConfig(params);
            window.setMovable(true);
            window.setTransparent(true);
            view.setTouchFocusable(true);
            view.setFocusBorderEnable(true);
        }else {
            commonDialog = new CommonDialog(context);
            commonDialog.setMovable(true);
            commonDialog.setSize(800, 450);
            commonDialog.getWindow().setTransparent(false);
            view.setHeight(450);
            view.setWidth(800);
            commonDialog.setContentCustomComponent(view);
            commonDialog.show();
        }
        return true;
    }

    public void quieWindowFloat() {
        if (floatParams.systemFloat){
            if (window != null){
                window.clearWindowFlags(params.flags);
                WindowManager.getInstance().destroyWindow(window);
            }
        }else {
            commonDialog.setSwipeToDismiss(true);
            newFloatParams = null;
            commonDialog = null;
        }

    }

    public FloatParams getFloatParams() {
        return floatParams;
    }

    //检查浮窗权限
    public boolean checkPermission() {
        return AppSettings.canShowFloating(context);
    }
}

